/**
 *\brief 字符串基本操作
 */
#ifndef _zString_H
#define _zString_H

#include <string>
#include <algorithm>
#include <cctype>

namespace zBase
{
	template <typename Container>
	inline void stringtok(Container &container,const std::string &in,
			const char *const token=" \t\n",const int deep=0)
	{
		const std::string::size_type len = in.length();
		std::string::size_type i = 0;
		int count = 0;
		while(i < len)
		{
			i = in.find_first_not_of(token,i);	
			if(std::string::npos == i)
				return;
			std::string::size_type j = in.find_first_of(token,i);
			++count;
			if(std::string::npos == j || (deep && count > deep))
			{
				container.push_back(in.substr(i));
				return;
			}
			else
				container.push_back(in.substr(i,j-1));

			//continue serch
			i = j+1;
		}
	}

	inline std::string & replace_all(std::string &str,const std::string &old_value,const std::string &new_value)
	{
		while(true)
		{
			std::string::size_type pos(0);
			if(std::string::npos != (pos = str.find(old_value)))
				str.replace(pos,old_value.length(),new_value);
			else
				break;
		}
		return str;
	}

	struct ToLower
	{
		char operator()(char c)const
		{
			std::tolower(c);
		}
	};
	inline void to_lower(std::string &s)
	{
		std::transform(s.begin(),s.end(),s.begin(),ToLower());
	}

	struct ToUpper
	{
		char operator()(char c)const
		{
			std::toupper(c);
		}
	};
	inline void to_upper(std::string &s)
	{
		std::transform(s.begin(),s.end(),s.begin(),ToUpper());
	}
}

#endif
